#include "calendaritem.h"
#include <QDebug>

CalendarItem::CalendarItem(QWidget *parent) : QWidget(parent)
{
    hover = false;
    pressed = false;
    listDayName << "*" << "初一" << "初二" << "初三" << "初四" << "初五" << "初六" << "初七"
                << "初八" << "初九" << "初十" << "十一" << "十二" << "十三" << "十四" << "十五"
                << "十六" << "十七" << "十八" << "十九" << "二十" << "廿一" << "廿二" << "廿三"
                << "廿四" << "廿五" << "廿六" << "廿七" << "廿八" << "廿九" << "三十";
    select = false;
    showLunar = true;
    bgImage = ":/res/bg_calendar.png";
    selectType = SelectType_Rect;

    date = QDate::currentDate();
    lunar = "初一";
    dayType = DayType_MonthCurrent;

    borderColor = QColor(180, 180, 180);
    weekColor = QColor(255, 0, 0);
    superColor = QColor(255, 29, 6);
    lunarColor = QColor(55, 156, 238);

    currentTextColor = QColor(0, 0, 0);
    otherTextColor = QColor(200, 200, 200);
    selectTextColor = QColor(255, 255, 255);
    hoverTextColor = QColor(250, 250, 250);

    currentLunarColor = QColor(150, 150, 150);
    otherLunarColor = QColor(200, 200, 200);
    selectLunarColor = QColor(255, 255, 255);
    hoverLunarColor = QColor(250, 250, 250);

    currentBgColor = QColor(255, 255, 255);
    otherBgColor = QColor(240, 240, 240);
    selectBgColor = QColor(208, 47, 18);
    hoverBgColor = QColor(204, 183, 180);
}

void CalendarItem::setDate(const QDate &date, const QString &lunar, const DayType &dayType)
{
    this->date = date;
    this->lunar = lunar;
    this->dayType = dayType;
    this->update();
}

void CalendarItem::setSelect(bool select)
{
    if(this->select != select)
    {
        this->select = select;
        this->update();
    }
}

void CalendarItem::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    drawBg(&painter);

    if(select)
        drawBgCurrent(&painter, selectBgColor);
    else if(hover)
        drawBgCurrent(&painter, hoverBgColor);

    drawDay(&painter);
    drawLunar(&painter);
}

void CalendarItem::drawBg(QPainter *painter)
{
    painter->save();

    QColor bgColor = currentBgColor;
    if(dayType == DayType_MonthPre || dayType == DayType_MonthNext)
        bgColor = otherBgColor;

    painter->setPen(borderColor);
    painter->setBrush(bgColor);
    painter->drawRect(rect());

    painter->restore();
}

void CalendarItem::drawDay(QPainter *painter)
{
    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);
    painter->save();

    QColor color = currentTextColor;
    if(dayType == DayType_MonthPre || dayType == DayType_MonthNext)
    {
        color = otherTextColor;
    }
    else if(dayType == DayType_WeekEnd)
        color = weekColor;

    if(select)
        color = selectTextColor;
    else if(hover)
        color = hoverTextColor;

    painter->setPen(color);

    if(showLunar)
    {
        QFont font;
        font.setPixelSize(side/2.7);
        painter->setFont(font);

        QRect dayrect = QRect(0, 0, width, height/1.7);
        painter->drawText(dayrect, Qt::AlignHCenter|Qt::AlignBottom, QString::number(date.day()));
    }
    else
    {
        QFont font;
        font.setPixelSize(side/2);
        painter->setFont(font);

        QRect dayRect = QRect(0, 0, width, height);
        painter->drawText(dayRect, Qt::AlignCenter, QString::number(date.day()));
    }
    painter->restore();
}

void CalendarItem::drawLunar(QPainter *painter)
{
    if(!showLunar)
        return ;
    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);

    painter->save();

    bool exist = (!listDayName.contains(lunar) && dayType!=DayType_MonthPre && dayType!=DayType_MonthNext);

    QColor color = currentLunarColor;
    if(dayType == DayType_MonthPre || dayType == DayType_MonthNext)
        color = otherLunarColor;
    if(select)
        color = selectTextColor;
    else if(hover)
        color = hoverTextColor;
    else if(exist)
    {
        color = lunarColor;
    }

    painter->setPen(color);
    QFont font;
    font.setPixelSize(side/5);
    painter->setFont(font);

    QRect lunarRect(0, height/2, width, height/2);
    painter->drawText(lunarRect, Qt::AlignCenter, lunar);

    painter->restore();
}

void CalendarItem::drawBgCurrent(QPainter *painter, QColor c)
{
    int width = this->width();
    int height = this->height();

    int side = qMin(width, height);
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(c);

    if(selectType == SelectType_Rect)
        painter->drawRect(rect());
    else if(selectType == SelectType_Circle)
    {
        int radius = side/2-3;
        painter->drawEllipse(QPoint(width/2, height/2), radius, radius);
    }
    else if(selectType == SelectType_Triangle)
    {
        int radius = side/3;
        QPolygon pts;
        pts.setPoints(3, 1, 1, radius, 1, 1, radius);
        painter->drawRect(rect());
        painter->setBrush(superColor);
        painter->drawConvexPolygon(pts);
    }
    else if(selectType == SelectType_Image)
    {
        QImage img(bgImage);
        if(!img.isNull())
        {
            img = img.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
            int x = (width - img.width()) / 2;
            int y = (height - img.height()) / 2;
            painter->drawImage(x, y, img);
        }
    }
    painter->restore();
}

void  CalendarItem::enterEvent(QEvent *)
{
    hover = true;
    update();
}

void CalendarItem::leaveEvent(QEvent *)
{
    hover = false;
    this->update();
}

void CalendarItem::mousePressEvent(QMouseEvent *)
{
    pressed = true;
    update();

    emit clicked(date, dayType);
}

void CalendarItem::mouseReleaseEvent(QMouseEvent *)
{
    pressed = false;
    update();
}
